﻿var urlCard;
function GetPageFunction() {
    if (typeof (adFunctions) != 'undefined') {
        var functions = adFunctions.split(',');
        for (var index = 0; index < functions.length; index++) {
            if (typeof (eval(functions[index])) === 'function') {
                if (functions[index].match('^tbar[a-zA-Z]*\_')) {
                    BuildFunction(functions[index]);
                }
                else if (functions[index].match('^grd[a-zA-Z]*\_')) {
                    BuildGridAction(functions[index]);
                }
                else if (functions[index].match('\_')) {
                    BuildFieldAction(functions[index]);
                }
            }
        }
    }
    if (typeof (SetTextForControl) != 'undefined')
        SetTextForControl();
}
function BuildFieldAction(name) {
    var paths = name.split('_');

    if (paths.length > 2) {
        var path0 = '';
        for (var ix = 0; ix < paths.length - 2; ix++) {
            path0 += paths[ix] + '_';
        }
        path0 += paths[paths.length - 2];
        paths[0] = path0;
        paths[1] = paths[paths.length - 1];
    }
    var bt = Ext.getCmp(paths[0]);
    if (typeof (bt) != 'undefined') {
        //if (!bt.hasListener(paths[1]))
        bt.addListener(paths[1], eval(name));
    }
}
function BuildFunction(name) {
    var paths = name.split('_');
    var bt = Ext.getCmp(paths[0]);
    if (typeof (bt) != 'undefined') {
        eval(paths[0] + '.' + paths[1] + '=' + name);
    }
    else {
        bt = new Ext.Button({ id: paths[0], hidden: false, text: paths[0] });
        eval(paths[0] + '.' + paths[1] + '=' + name);

        // Insert separator before button action.
        var sep = new Ext.Toolbar.Separator({ id: "tbSep_" + name.split('_')[0].replace('tbar', '') });
        tbarMain.items.insert(1, sep);
        //tbarMain.items.get('tbSep_Release')

        // Insert button action.
        tbarMain.items.insert(2, bt);

        tbarMain.syncSize();
    }
    ViewPort1.syncSize();
}
function BuildGridAction(name) {
    var paths = name.split('_');
    var bt = Ext.getCmp(paths[0]);
    if (typeof (bt) != 'undefined') {
        if (!bt.hasListener(paths[1]))
            bt.addListener(paths[1], eval(name));
    }
}
var OpenCard = function(mode) {
    var query = window.location.search.substring(1);
    var aky = '';
    var filter = '';
    var no = '';
    var record = null;
    if (mode == 'edit' || mode == 'delete') {
        if (grpList.hasSelection()) {
            record = grpList.getSelectionModel().getSelections()[0];
            no = encodeURIComponent(record.get('Key'));
            if (query.indexOf('aky') >= 0) {
                var akys = query.split('aky=')[1].split('&')[0].split('!');
                for (id = 0; id < akys.length; id++) {
                    aky = aky + akys[id] + ':' + record.get(akys[id]) + '!';
                }
            }
            if (query.indexOf('filter') >= 0) {
                var filters = query.split('filter=')[1].split('&')[0].split('|');
                for (id = 0; id < filters.length; id++) {
                    filter = filter + filters[id] + ':' + record.get(filters[id]) + '|';
                }
            }
        }
        else {
            Ext.Msg.alert(TXTMessage, TXTNolineSelect);
        }
    }
    var url = String.format(urlCard, no, mode);
    if (aky != '') {
        url = url + "&aky=" + aky;
    }
    if (filter != '') {
        url = url + 'filter=' + filter;
    }
    var at = '';
    if (typeof (record) != 'undefined' && record != null)
        at = record.get(record.store.fields.items[1].name);
    if (url != '') {
        //dailt set value from list
        if (typeof (LoadSpecialValueforList) != 'undefined') {
            LoadSpecialValueforList();
            if (tableName == "DMSPhysJournal") {
                var ads = grdLines;
                ads.colModel.columns[0].editable = false;
                ads.getColumnModel().isCellEditable = function(col, row) {
                    var record = ads.store.getAt(row);
                    if (col == '1') { // replace with your condition
                        //if (e.getColumnModel().getColumnAt(col).dataIndex == "No_Series") {
                        return false;
                    }
                    return Ext.grid.ColumnModel.prototype.isCellEditable.call(this, col, row);
                }
            }
            load(url, at);
        }
        else
            load(url, at);
    }
}

function reloadFrame() {

    var windowParent = window.parent.Ext.getCmp(this.name.replace('_IFrame', ''));
    windowParent.load(windowParent.autoLoad.url);
}

function Lookup(el, trigger, index, e, url) {
    var wnd = Ext.getCmp('wndLookUp');
    ctrlLookup = el;
    if (wnd.autoLoad.url == url) {
        wnd.show(); return;
    }
    wnd.setTitle(TXTFind + el.fieldLabel.toLowerCase());
    wnd.load(url);
    wnd.center();

    wnd.show();

}
var RowDoubleClick = function(grid, rowIndex, e) {

    var record = grid.getStore().getAt(rowIndex);
    var query = window.location.search.substring(1);
    if (query.indexOf('met=468') >= 0)
        return;
    var aky = '';
    var filter = '';
    if (query.indexOf('aky') >= 0) {
        var akys = query.split('aky=')[1].split('&')[0].split('!');
        for (id = 0; id < akys.length; id++) {
            aky = aky + akys[id] + ':' + record.get(akys[id]) + '|';
        }
    }
    if (query.indexOf('filter') >= 0) {
        var filters = query.split('filter=')[1].split('&')[0].split('|');
        for (id = 0; id < filters.length; id++) {
            filter = filter + filters[id] + ':' + record.get(filters[id]) + '|';
        }
    }
    var url = String.format(urlCard, encodeURIComponent(record.get('Key')), 'view');
    if (aky != '') {
        url = url + "&aky=" + aky;
    }
    if (filter != '') {
        url = url + 'filter=' + filter;
    }

    load(url, record.get(grid.getStore().fields.items[1].name));
    return;

}

